package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import entities.Calendar;
import exceptions.IllegalAppointmentException;
import fachlogik.Administration;
import fachlogik.CalendarCellRenderer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JComboBox;

public class EditCalendarWindow extends JFrame {

	private JPanel contentPane;
	private JTextField tf_name;
	private DefaultComboBoxModel dcm;

	private Calendar selectedCalendar;
	private Administration instance;
	private CalendarWindow cw;

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// EditCalendarWindow frame = new EditCalendarWindow();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/**
	 * Create the frame.
	 */
	public EditCalendarWindow(Calendar c, final CalendarWindow cw) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				cw.seteCW(null);
			}
		});

		dcm = new DefaultComboBoxModel();
		setTitle("Calendar");
		setVisible(true);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 237, 275);
		setLocationRelativeTo(null);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		selectedCalendar = c;
		this.cw = cw;
		instance = cw.getInstance();

		if (selectedCalendar != null) {
			JButton btnSaveCalendar = new JButton("Save Calendar");
			btnSaveCalendar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (!tf_name.getText().equals("")) {
						selectedCalendar.setName(tf_name.getText());
						instance.editCalendar(selectedCalendar);
						cw.calendarsAktualisieren();
						cw.seteCW(null);
						dispose();
					} else
						JOptionPane.showMessageDialog(null,
								"The calendar name must not be empty!");
				}
			});
			btnSaveCalendar.setBounds(5, 97, 206, 25);
			contentPane.add(btnSaveCalendar);
		}

		JButton btnSaveAsNew = new JButton("Save As New Calendar");
		btnSaveAsNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!tf_name.getText().equals("")) {
					Calendar c = new Calendar();
					c.setName(tf_name.getText());
					c.setPerson(instance.getUser());
					instance.addCalendar(c);
					cw.calendarsAktualisieren();
					cw.seteCW(null);
					dispose();
				} else
					JOptionPane.showMessageDialog(null,
							"Please enter a name for your new calendar!");
			}
		});
		btnSaveAsNew.setBounds(5, 133, 206, 25);
		contentPane.add(btnSaveAsNew);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cw.seteCW(null);
				dispose();
			}
		});
		btnCancel.setBounds(5, 203, 206, 23);
		contentPane.add(btnCancel);

		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(5, 72, 46, 14);
		contentPane.add(lblName);

		tf_name = new JTextField();
		tf_name.setBounds(73, 66, 138, 20);
		contentPane.add(tf_name);
		tf_name.setColumns(10);

		if (selectedCalendar != null) {
			JButton btnDeleteCalendar = new JButton("Delete Calendar");
			btnDeleteCalendar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						instance.removeCalendar(selectedCalendar);
						cw.calendarsAktualisieren();
						cw.seteCW(null);
						dispose();
					} catch (IllegalAppointmentException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
			btnDeleteCalendar.setBounds(5, 169, 206, 23);
			contentPane.add(btnDeleteCalendar);
		}

		cbAktualisieren();
		final JComboBox cb_calendar = new JComboBox(dcm);
		cb_calendar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectedCalendar = (Calendar) cb_calendar.getSelectedItem();
				tfBefuellen();
			}
		});
		cb_calendar.setRenderer(new CalendarCellRenderer());
		cb_calendar.setBounds(5, 35, 211, 20);
		contentPane.add(cb_calendar);
		if (dcm.getElementAt(0) != null) {
			selectedCalendar = (Calendar) dcm.getElementAt(0);
			tfBefuellen();
		}

		JLabel lblChooseACalendar = new JLabel("Choose a calendar:");
		lblChooseACalendar.setBounds(5, 10, 129, 14);
		contentPane.add(lblChooseACalendar);
	}

	private void cbAktualisieren() {
		for (Calendar c : instance.getUserCalendar())
			dcm.addElement(c);
	}

	private void tfBefuellen() {
		if (selectedCalendar != null)
			tf_name.setText(selectedCalendar.getName());
	}
}
